-- ============================================
-- INTRO - SNOUB CASINO
-- Chat qui tourne (spin plus fluide), transformation en piece
-- doree, texte CASINO qui apparait, avec une petite musique
-- de casino en boucle (necessite un Speaker a cote du computer).
-- ============================================

local mon = peripheral.find("monitor")
if not mon then
    error("Aucun monitor trouve ! Colle un Advanced Monitor a cote du computer.")
end

local speaker = peripheral.find("speaker")  -- optionnel, l'anim marche sans

mon.setTextScale(1.45)
local prevTerm = term.redirect(mon)
local w, h = term.getSize()

local function clear()
    term.setBackgroundColor(colors.black)
    term.clear()
end

local function centerText(y, text, textColor)
    term.setBackgroundColor(colors.black)
    term.setTextColor(textColor or colors.white)
    local x = math.floor((w - #text) / 2) + 1
    term.setCursorPos(x, y)
    term.write(text)
end

local function drawLines(lines, color, startY)
    for i, line in ipairs(lines) do
        centerText(startY + i - 1, line, color)
    end
end

local function eraseLines(lines, startY)
    for i, line in ipairs(lines) do
        centerText(startY + i - 1, string.rep(" ", #line), colors.black)
    end
end

-- === MUSIQUE DE CASINO (boucle, jouee en parallele) ===
local MELODY = {
    { note = "harp", pitch = 12, dur = 0.15 },
    { note = "harp", pitch = 14, dur = 0.15 },
    { note = "harp", pitch = 16, dur = 0.15 },
    { note = "bell", pitch = 19, dur = 0.30 },
    { note = "harp", pitch = 16, dur = 0.15 },
    { note = "harp", pitch = 14, dur = 0.15 },
    { note = "harp", pitch = 12, dur = 0.30 },
    { note = "harp", pitch = 9,  dur = 0.15 },
    { note = "harp", pitch = 12, dur = 0.15 },
    { note = "bell", pitch = 16, dur = 0.40 },
    { note = "harp", pitch = 19, dur = 0.15 },
    { note = "harp", pitch = 17, dur = 0.15 },
    { note = "harp", pitch = 14, dur = 0.15 },
    { note = "bell", pitch = 12, dur = 0.50 },
}

local function playMusic()
    if not speaker then return end
    while true do
        for _, n in ipairs(MELODY) do
            speaker.playNote(n.note, 1, n.pitch)
            sleep(n.dur)
        end
    end
end

-- === FRAMES DU CHAT QUI TOURNE (spin plus fluide, 8 poses) ===
local CAT_FRAMES = {
    { " /\\_/\\ ", "( o.o )", " > ^ < " },   -- face
    { " /\\_/ ",  "( o.o )", " > ^ <  " },   -- leger 3/4
    { " /\\_/  ", "( o.o) ", " >^<    " },   -- 3/4
    { "  /|   ",  "  o|   ", "  >|   " },    -- presque de profil
    { "   |   ",  "   |   ", "   |   " },    -- de profil pur (fin de rotation)
    { "  |\\   ",  "  |o   ", "  |<   " },   -- 3/4 autre sens (debut retour)
    { " \\_/\\  ", " (o.o) ", "  >^<  " },   -- 3/4
    { " \\_/\\ ",  "( o.o )", " > ^ < " },   -- face (boucle, dos a nous puis retour)
}

local function playCatSpin(cycles)
    local startY = math.floor(h / 2) - 3
    centerText(startY - 2, "=== SNOUB CASINO ===", colors.yellow)

    for c = 1, cycles do
        for _, frame in ipairs(CAT_FRAMES) do
            drawLines(frame, colors.orange, startY)
            sleep(0.08)
            eraseLines(frame, startY)
        end
    end
end

-- === TRANSFORMATION EN PIECE DOREE ($) ===
local COIN_STEPS = {
    { "  $  " },
    { " ___ ", "( $ )", " --- " },
    { "  ___  ", " /   \\ ", "|  $  |", " \\___/ " },
}

local function playCoinGrow()
    local startY = math.floor(h / 2) - 3
    for _, step in ipairs(COIN_STEPS) do
        drawLines(step, colors.yellow, startY)
        sleep(0.35)
        if step ~= COIN_STEPS[#COIN_STEPS] then
            eraseLines(step, startY)
        end
    end
    sleep(0.6)
    eraseLines(COIN_STEPS[#COIN_STEPS], startY)
end

-- === REVELATION DU TEXTE CASINO ===
local function playCasinoReveal()
    local text = "C A S I N O"
    local y = math.floor(h / 2)
    local x = math.floor((w - #text) / 2) + 1

    term.setBackgroundColor(colors.black)
    for i = 1, #text do
        local ch = text:sub(i, i)
        if ch ~= " " then
            term.setTextColor(colors.yellow)
            term.setBackgroundColor(colors.gray)
            term.setCursorPos(x + i - 1, y)
            term.write(ch)
            if speaker then speaker.playNote("bell", 1, 18) end
            sleep(0.09)
            term.setBackgroundColor(colors.black)
            term.setTextColor(colors.yellow)
            term.setCursorPos(x + i - 1, y)
            term.write(ch)
        else
            sleep(0.05)
        end
    end

    sleep(0.4)
    centerText(y + 2, "SNOUB CASINO", colors.lime)
end

-- === SEQUENCE PRINCIPALE (visuel) ===
local function mainSequence()
    clear()
    playCatSpin(3)
    playCoinGrow()
    clear()
    centerText(2, "=== SNOUB CASINO ===", colors.yellow)
    playCasinoReveal()

    sleep(2.5)
    clear()
    centerText(math.floor(h / 2), "Bienvenue au Snoub Casino !", colors.lime)
    sleep(2)
end

-- === PROGRAMME PRINCIPAL (boucle en continu) ===
-- Astuce : Ctrl+T sur le computer pour arreter l'animation.
if speaker then
    while true do
        parallel.waitForAny(playMusic, mainSequence)
    end
else
    while true do
        mainSequence()
    end
end

term.redirect(prevTerm)
